﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

using Qplus.Infrastructure;
using Qplus.beans;
using Qplus.enums;
using Qplus.protocol;

namespace Qplus.UI
{
    public class LoginHelper : IPacketListener
    {
        String version = "1000";
        public LoginHelper()
        {
            
        }

        public void onLoginVerify(int result, string ip, int port,
            byte[] key, long userID, String upurl)
        {
            QUser user = QplusT.getInstance().getUser();
            switch (result)
            {
                case 0:
                    if (user != null)
                    {
                        user.key = key;
                        //user.setKey(key);
                        user.getInfo().setUserID(userID);
                        user.url = upurl;
                 
                    }
                    QplusT.getInstance().loginUserServer(ip, port, version, "", key, userID);
                    Debug.WriteLine("验证成功");
                    break;
                case 1:
                    QplusT.changeLoginState(user, false, false);
                    //			if(user != null){
                    //				user.setKey(null);
                    //				user.setUserID(-1);
                    //				user.notifyObservers();
                    //			}
                    ConnectionManager.getInstance().closeConnection();
                    Debug.WriteLine("", "验证失败");
                    break;

                default:
                    break;
            }
        }

        public void onLogout(int result)
        {
            ConnectionManager.getInstance().closeConnection();
            QUser user = QplusT.getInstance().getUser();
            QplusT.changeLoginState(user, false, false);
            //KeepAlive.stopKeepAlive();
        }


        public void onLoginUserServer(int result, long appID, E_USER_LEVEL level)
        {
            QUser user = QplusT.getInstance().getUser();
            switch (result)
            {
                case 0:
                    user.appID = appID;
                    user.getInfo().userLevel = level;
                    //user.getInfo().notifyObservers();
                    QplusT.changeLoginState(user, false, true);
                    Debug.WriteLine("登陆成功");
                    user.isForceLogout = false;
                    break;
                case 1:
                    QplusT.changeLoginState(user, false, false);

                    Debug.WriteLine("登录失败");
                    break;

                default:
                    break;
            }
        }

	    public virtual void packetEvent(PacketEvent Pevent) {
		    InPacket packet = Pevent.myPacket;
		    if(packet == null){
			    return;
            }   
		    //验证
		    else if(packet is L2C_RESP_LOGIN){
			    L2C_RESP_LOGIN p = ((L2C_RESP_LOGIN) packet);
			    onLoginVerify(p.getResult(), p.getIP(),
						    p.getPort(), p.getKey(), p.getUserID(),
						    p.getUpgradeURL());
		    }
		    //登陆用户服务器
		    else if(packet is U2C_RESP_LOGIN){
			    U2C_RESP_LOGIN p = ((U2C_RESP_LOGIN) packet);
			    E_USER_LEVEL level = 0;
			    int l = p.getUserLevel();
			    if (l == (int)E_USER_LEVEL.EUL_GENERAL) {
				    level = E_USER_LEVEL.EUL_GENERAL;
			    } else if (l == (int)E_USER_LEVEL.EUL_SENIOR) {
				    level = E_USER_LEVEL.EUL_SENIOR;
			    } else if (l == (int)E_USER_LEVEL.EUL_CS) {
				    level = E_USER_LEVEL.EUL_CS;
			    }
			    onLoginUserServer(p.getResult(), p.getAppID(),
						    level);
		    }
		    //退出登录
		    else if(packet is U2C_RESP_LOGOUT){
			    U2C_RESP_LOGOUT p = ((U2C_RESP_LOGOUT) packet);
			    onLogout(p.getResult());
		    }
	    }


    }
}
